********************************************************************************
*PROJECT: Legislature Integration and Bipartisanship: A Natural Experiment in Iceland								   
*PURPOSE: Prep MP-session level data for both cosponsorship and voting
********************************************************************************

clear all
set mem 1g
set maxvar 32000
set more off
local filefolder = "$path_pch\Data\raw\Voting"
local cosponfolder = "$path_pch\Data\raw\Co-Sponsorship"
local seatingfolder = "$path_pch\Data\intermediate\Seating"
local votingfolder = "$path_pch\Data\raw\Voting"
local votingintfolder = "$path_pch\Data\intermediate\Voting"
local votinganalyfolder = "$path_pch\Data\analysis\Voting"
local tablefolder = "$path_pch\Output\Tables"
local graphfolder = "$path_pch\Output\Graphs"
local mpfolder = "$path_pch\Data\raw\MP"
local mpintfolder = "$path_pch\Data\intermediate\MP"
local runsRI = 1000
tempfile vote_billWeight voteLHS voteLHSLag voteLHSLead MP_bio cosponbill cosponres ///
	cosponrep cosponLag cosponLead cosponLHS voting_VS voting_VVS VS_Vote_p25 VS_Vote_p50


*********************************************************************
// LHS: Vote
*********************************************************************

// Bring voting data created from Saia_prep
use "`votinganalyfolder'/voting_analysis", clear

// Calculate modal vote
keep vote_id vote
bys vote_id vote: gen NumVote = _N
bys vote_id: egen ModalVoteCount = max(NumVote)
g ModalVotetmp = vote if NumVote==ModalVoteCount
bys vote_id: egen ModalVote = mode(ModalVotetmp)
drop ModalVotetmp
bys vote_id: gen totalvote = _N

// collapse, draw histogram
keep vote_id ModalVoteCount ModalVote totalvote
duplicates drop
gen ModalVoteShare = ModalVoteCount / totalvote
sum ModalVoteShare, d

foreach p in p50 p25 {
	preserve
		keep if ModalVoteShare < r(`p')
		keep vote_id
		dis _N
		save `VS_Vote_`p''
	restore
}

// Bring voting data created from Saia_prep
use "`votinganalyfolder'/voting_analysis", clear
keep vote_id session_id bill_id MP_id non_compliance non_compliance_saia ///
	non_compliance_alt vote chairman_vote leader_vote divergent_peersRI0 ///
	divergent_peersLRRI0 divergent_peers_ivRI0 divergent_peersLR_ivRI0 ///
	party_leader_id

// Remove RI0
foreach v of varlist *RI0 {
	local vtrim = substr("`v'",1,length("`v'")-3)
	ren `v' `vtrim'
}

// create vote similarity measure
gen voteSim4c = 3-abs(3*(vote=="yes")+2*(vote=="absent")+1*(vote=="abstain")-3*(leader_vote=="yes")-2*(leader_vote=="absent")-1*(leader_vote=="abstain")) if !mi(vote) & !mi(leader_vote) & MP_id!=party_leader_id
gen voteSim3c = 2-abs((vote=="yes")-(vote=="no")-(leader_vote=="yes")+(leader_vote=="no")) if !mi(vote) & !mi(leader_vote) & MP_id!=party_leader_id

// old measure using chair of parliamentary group's vote
gen voteSim4c_saia = 3-abs(3*(vote=="yes")+2*(vote=="absent")+1*(vote=="abstain")-3*(chairman_vote=="yes")-2*(chairman_vote=="absent")-1*(chairman_vote=="abstain")) if !mi(vote) & !inlist(chairman_vote,"","unknown")
gen voteSim3c_saia = 2-abs((vote=="yes")-(vote=="no")-(chairman_vote=="yes")+(chairman_vote=="no")) if !mi(vote) & !inlist(chairman_vote,"","unknown")

// create rebel measure
g rebelStrong = (vote=="yes" & leader_vote=="no") | (vote=="no" & leader_vote=="yes") if !mi(vote) & !mi(leader_vote) & MP_id!=party_leader_id
g rebelWeak = (vote=="yes" & leader_vote=="no") | (vote=="no" & leader_vote=="yes") | ///
		(vote=="abstain" & leader_vote=="yes") | (vote=="abstain" & leader_vote=="no") if !mi(vote) & !mi(leader_vote) & MP_id!=party_leader_id

// absence
g absent = vote=="absent" if !mi(vote)
g abstain = vote=="abstain" if !mi(vote)
g absentORabstain = inlist(vote,"absent","abstain") if !mi(vote)
drop vote chairman_vote leader_vote

// Collapse to get measures: contentious votes (VS, using p50 ~= .66), very contentious votes (VVS, using p25 ~= .52)
forv k = 1/2 {
	local p: word `k' of "p25" "p50"
	local disc: word `k' of "VVS" "VS"
	preserve
		merge n:1 vote_id using `VS_Vote_`p'', nogen keep(3) // keep only contentious votes
		drop vote_id // Drop unnecessary variables
		collapse (mean) non_compliance non_compliance_saia non_compliance_alt voteSim?c voteSim?c_saia rebelStrong rebelWeak absent, by(session_id bill_id MP_id) // each bill gets the same weight, so collapse by bill first.
		drop bill_id
		collapse (mean) non_compliance non_compliance_saia non_compliance_alt voteSim?c voteSim?c_saia rebelStrong rebelWeak absent, by(session_id MP_id) // now collapse to session-MP-level
		foreach var of varlist non_compliance non_compliance_saia non_compliance_alt voteSim?c voteSim?c_saia rebelStrong rebelWeak absent {
			ren `var' `var'_`disc'
		}
		save `voting_`disc''
	restore 
}

// First, collapse for equal weight for each bill
preserve
	drop vote_id
	collapse (mean) non_compliance non_compliance_saia non_compliance_alt voteSim?c voteSim?c_saia rebelStrong rebelWeak divergent_peers* absent abstain absentORabstain, by(session_id bill_id MP_id)
	drop bill_id
	collapse (mean) non_compliance non_compliance_saia non_compliance_alt voteSim?c voteSim?c_saia rebelStrong rebelWeak divergent_peers* absent abstain absentORabstain, by(session_id MP_id)
	save `vote_billWeight'
restore

// Second, collapse for equal weight for each vote
foreach v of varlist non_compliance non_compliance_saia non_compliance_alt rebelStrong rebelWeak voteSim?c voteSim?c_saia absent abstain absentORabstain {
	ren `v' `v'_Vwgt
}

drop bill_id
collapse (mean) *_Vwgt, by(session_id MP_id)

// merge preferred weight variables
merge 1:1 session_id MP_id using `vote_billWeight', nogen
merge 1:1 session_id MP_id using `voting_VVS', nogen
merge 1:1 session_id MP_id using `voting_VS', nogen

la var non_compliance "frac. voted differently to party leader, weight by bill"
la var non_compliance_saia "frac. voted differently to chair of parliamentary group (as in Saia), weight by bill"
la var non_compliance_alt "frac. voted differently to modal party vote, weight by bill"
la var non_compliance_Vwgt "frac. voted differently to party leader, weight by vote"
la var non_compliance_saia_Vwgt "frac. voted differently to chair of parliamentary group (as in Saia), weight by vote"
la var non_compliance_alt_Vwgt "frac. voted differently to modal party vote, weight by vote"
la var voteSim4c "ave. 4 Cat vote similarity (3 = same vote, 0 = yes-no), weight by bill"
la var voteSim3c "ave. 3 Cat vote similarity (1 = same vote, -1 = yes-no), weight by bill"
la var voteSim4c_saia "ave. 4 Cat vote similarity (3 = same vote, 0 = yes-no) (Saia chair), weight by bill"
la var voteSim3c_saia "ave. 3 Cat vote similarity (1 = same vote, -1 = yes-no) (Saia chair), weight by bill"
la var voteSim4c_Vwgt "ave. 4 Cat vote similarity (3 = same vote, 0 = yes-no), weight by vote"
la var voteSim3c_Vwgt "ave. 3 Cat vote similarity (1 = same vote, -1 = yes-no), weight by vote"
la var voteSim4c_saia_Vwgt "ave. 4 Cat vote similarity (3 = same vote, 0 = yes-no) (Saia chair), weight by vote"
la var voteSim3c_saia_Vwgt "ave. 3 Cat vote similarity (1 = same vote, -1 = yes-no) (Saia chair), weight by vote"
la var rebelStrong "frac. voted no (yes) when party leader voted yes (no), weight by bill"
la var rebelStrong_Vwgt "frac. voted no (yes) when party leader voted yes (no), weight by vote"
la var rebelWeak "frac. voted no/abstain (yes/abstain) when party leader voted yes (no), weight by bill"
la var rebelWeak_Vwgt "frac. voted no/abstain (yes/abstain) when party leader voted yes (no), weight by vote"
la var absent "frac. absent from voting, weight by bill"
la var absent_Vwgt "frac. absent from voting, weight by vote"
la var abstain "frac. abstain from voting, weight by bill"
la var abstain_Vwgt "frac. abstain from voting, weight by vote"
la var absentORabstain "frac. abstain/absent from voting, weight by bill"
la var absentORabstain_Vwgt "frac. abstain/absent from voting, weight by vote"
save `voteLHS'

// Prep lead and lag variables
forv k = 1/2 {
	local l: word `k' of "1" "-1"
	local t: word `k' of "Lag" "Lead"
	use `voteLHS', clear
	replace session_id = session_id + `l'
	foreach var of varlist non_compliance* voteSim* rebel* divergent_peers* absent* abstain* {
		ren `var' `var'`t'
	}
	save `voteLHS`t''
}

// Merge lead and lag
use `voteLHS', clear
merge 1:1 MP_id session_id using `voteLHSLag', nogen
merge 1:1 MP_id session_id using `voteLHSLead', nogen

// Create compliance variable (non-compliance reverse-coded)
foreach var of varlist non_compliance* {
	local newname = substr("`var'",5,.)
	g `newname' = 1 - `var'
}

// Create vote difference variable (vote similarity reverse-coded)
foreach var of varlist voteSim4* {
	local newname = substr("`var'",8,.)
	g voteDiff`newname' = 3 - `var'
}
foreach var of varlist voteSim3* {
	local newname = substr("`var'",8,.)
	g voteDiff`newname' = 2 - `var'
}

save `voteLHS', replace

*********************************************************************
// LHS: Co-sponsorship (bills, reports, resolutions, all)
*********************************************************************

// Prep party info of MPs
use "`mpintfolder'\MP_Bio.dta", clear
keep session_id MP_id party_id
save `MP_bio'

// Bring co-sponsorship data, calculate LHS variables (blls, reports, resolutions)
forv k = 1/3 {
	
	local x: word `k' of cosponsorship resolutions reports
	local expname: word `k' of bill resolution report
	local shortname: word `k' of bill res rep

	// Bring Scraped data (source for bills: http://www.althingi.is//thingstorf/thingmalalistar-eftir-thingum/lagafrumvorp/?lthing=115)
	import delimited using "`cosponfolder'\\`x'.csv", clear enc("utf-8") varn(1)

	// renaming
	ren sessionnum session_id
	ren mp_id MP_id

	// check unique identifier
	isid session_id billnum MP_id

	// keep relevant obs.
	drop if session_id==114 // no seating
	bys session_id billnum: g num_sponsor = _N
	
	// merge party_id
	merge n:1 session_id MP_id using `MP_bio', nogen keep(1 3)
	
	// for small number of non-matched, use the abbreviation to get party ID
	forv k = 1/20 {
		local ab: word `k' of A Ab F S SK U Þ JA Sf Óh Fl Vg Bhr Hr Bf V Flf M P None
		replace party_id = `k' if mp_party == "`ab'" & mi(party_id)
		// "utan flokka" means an MP with no party affiliation because he or she quit the party but stayed in parliament
		// "None" means a non-MP minister without party affiliation
	}
	assert !mi(party_id)
	
	// calculate number of bipartisan links within each bill
	bys session_id billnum party_id: gen cospon = num_sponsor - _N
	
	// calculate number of partisan links
	bys session_id billnum party_id: gen cosponSame = _N - 1
	
	// collapse to create session_MP level
	keep session_id MP_id cospon cosponSame
	collapse (sum) cospon cosponSame, by(session_id MP_id)
	gen ihs_cospon = ln(cospon + sqrt(1 + cospon^2))
	gen ihs_cosponSame = ln(cosponSame + sqrt(1 + cosponSame^2))
	la var cospon "Total # of `expname' links with other-party MPs"
	la var ihs_cospon "Inverse hyperbolic sine of cospon_`shortname'"
	la var cosponSame "Total # of `expname' links with same-party MPs"
	la var ihs_cosponSame "Inverse hyperbolic sine of cosponSame_`shortname'"
	ren cospon cospon_`shortname'
	ren ihs_cospon ihs_cospon_`shortname'
	ren cosponSame cosponSame_`shortname'
	ren ihs_cosponSame ihs_cosponSame_`shortname'
		
	// create lead and lag measures
	save `cospon`shortname''
	forv k = 1/2 {
		use `cospon`shortname'', clear
		local l: word `k' of "1" "-1"
		local t: word `k' of "Lag" "Lead"
		replace session_id = session_id + `l'
		foreach var of varlist *cospon* {
			ren `var' `var'`t'
		}
		save `cospon`t'', replace
	}
	use `cospon`shortname'', clear
	merge 1:1 session_id MP_id using `cosponLag', nogen keep(1 2 3)
	merge 1:1 session_id MP_id using `cosponLead', nogen keep(1 2 3)
	
	// save
	save `cospon`shortname'', replace
}

// merge all the cosponsor measures
use `cosponbill', clear
merge 1:1 session_id MP_id using `cosponres', nogen
merge 1:1 session_id MP_id using `cosponrep', nogen

// calculate sum of all.
foreach t in "" "Lag" "Lead" {
	egen cospon_all`t' = rowtotal(cospon_bill`t' cospon_res`t' cospon_rep`t')
	gen ihs_cospon_all`t' = ln(cospon_all`t' + sqrt(1 + cospon_all`t'^2))
	egen cosponSame_all`t' = rowtotal(cosponSame_bill`t' cosponSame_res`t' cosponSame_rep`t')
	gen ihs_cosponSame_all`t' = ln(cosponSame_all`t' + sqrt(1 + cosponSame_all`t'^2))	
	la var cospon_all`t' "Total # of all types of links with other-party MPs. `t'"
	la var ihs_cospon_all`t' "Inverse hyperbolic sine of cospon_all`t'"
	la var cosponSame_all`t' "Total # of all types of links with same-party MPs. `t'"
	la var ihs_cosponSame_all`t' "Inverse hyperbolic sine of cosponSame_all`t'"
}

save `cosponLHS'

*********************************************************************
// Bring data, merge LHS, calculate RHS variables, save data
*********************************************************************

// MPs of interest
use "`seatingfolder'\seating_MP_frontback.dta", clear
keep session_id MP_id gender party_id coalition strata ?_party_idRI* ///
	?_coalitionRI* ?_genderRI* age male const_sdate constituency_full constituency ///
	constName reykjavik southern government_party constOrder ever* ///
	firstSessionMember sessExper fixedPay wages expenses

// Bring LHS variables
merge 1:1 session_id MP_id using `voteLHS', nogen keep(1 3)
merge 1:1 session_id MP_id using `cosponLHS', nogen keep(1 3)

// Plug in zeros for cosponsorship variables when missing
foreach var of varlist *cospon* {
	replace `var' = 0 if mi(`var')
}


// RHS variables: other-party/coalition/gender neighbours
local j=1
foreach x in party_id coalition gender {

	if `j'==1 {
		local lab = "Party"
	}
	else {
		local lab = proper("`x'")
	}

	forv i=0/`runsRI' {
		g fracOther`lab'NeighborRI`i' = ( ((`x' != U_`x'RI`i') & !mi(U_`x'RI`i'))+((`x' != L_`x'RI`i') & !mi(L_`x'RI`i')) ) / ( (!mi(U_`x'RI`i'))+(!mi(L_`x'RI`i')) )
		assert (fracOther`lab'NeighborRI`i'<=1 & fracOther`lab'NeighborRI`i'>=0) if fracOther`lab'NeighborRI`i'!=.
		
		// Dummies for each amount
		g halfOther`lab'NeighborRI`i' = fracOther`lab'NeighborRI`i'==0.5 if fracOther`lab'NeighborRI`i'!=.
		g allOther`lab'NeighborRI`i' = fracOther`lab'NeighborRI`i'==1 if fracOther`lab'NeighborRI`i'!=.
		assert (halfOther`lab'NeighborRI`i'==0 | halfOther`lab'NeighborRI`i'==1 | halfOther`lab'NeighborRI`i'==.) & (allOther`lab'NeighborRI`i'==0 | allOther`lab'NeighborRI`i'==1 | allOther`lab'NeighborRI`i'==.)

		// Neighbours all round (including front and back)
		g fracOther`lab'NearRI`i' = ( ((`x' != U_`x'RI`i') & !mi(U_`x'RI`i'))+((`x' != L_`x'RI`i') & !mi(L_`x'RI`i'))+((`x' != F_`x'RI`i') & !mi(F_`x'RI`i'))+((`x' != B_`x'RI`i') & !mi(B_`x'RI`i')) ) / ( (!mi(U_`x'RI`i'))+(!mi(L_`x'RI`i'))+(!mi(F_`x'RI`i'))+(!mi(B_`x'RI`i')) )
		assert (fracOther`lab'NearRI`i'<=1 & fracOther`lab'NearRI`i'>=0) if fracOther`lab'NearRI`i'!=.

		// Interaction with experience
		g fracOther`lab'RI`i'XE = fracOther`lab'NeighborRI`i'*sessExper
		la var fracOther`lab'RI`i'XE "fracOther`lab'NeighborRI`i'*sessExper"		
	}
	local j=`j'+1
}


// Create lag and lead of main neighbour variable
foreach x in Party Coalition {
	sort MP_id session_id, stable
	by MP_id: g fracOth`x'NeighborRI0Lag = fracOther`x'NeighborRI0[_n-1]
	sort MP_id session_id, stable
	by MP_id: g fracOth`x'NeighborRI0Lead = fracOther`x'NeighborRI0[_n+1]
}

// Put missing for observations that are irrelevant (e.g., session 115's Lag variables)
	// See data/raw/session characteristics.xlsx for the list of sessions
foreach var of varlist *Lag {
	replace `var' = . if inlist(session_id,115,119,120,124,125,129,130,134,135,137,138,142,143,146,147,148,149)
}
foreach var of varlist *Lead {
	replace `var' = . if inlist(session_id,118,119,123,124,128,129,133,134,136,137,141,142,145,146,147,148,149)
}

// Other vars
gen specialSession = inlist(session_id,119,124,129,134,137,142)
gen shortSession = session_id==147
egen strataSessionPartyFE = group(strata session_id party_id)
drop ?_*RI*


// Save data
save "`votinganalyfolder'/MP_session_analysis", replace
beep